import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)
let car = JSON.parse(localStorage.getItem('shopCar')) ? JSON.parse(localStorage.getItem('shopCar')) : []
export default new Vuex.Store({
  state: {
    goods: car
  },
  mutations: {
    getGoodsList(state, info) {


      let flag = false;
      state.goods.some(
        item => {
          if (item.id == info.id) {
            item.count += parseInt(info.count)
            flag = true;
            return true;
          }
        }
      )
      if (!flag) {
        state.goods.push(info)
      }
      localStorage.setItem('shopCar', JSON.stringify(state.goods))
    }
  },
  actions: {

  },
  getters: {
    getAllCount(state) {
      console.log(state.goods);
      let c = 0;
      state.goods.forEach(item => {
        c += item.count
      })
      return c
    }
  }
})
